home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 1.0 for Developers / QuickTime 1.0 for Developers.iso / Programming Stuff / Sample Code / MiniPlayer / Mini Main.c next >
C/C++ Source or Header  |  1991-09-04  |  13KB  |  566 lines

  1. /**************************************************
  2. *
  3. * Sample Movie Player
  4. *
  5. *    This program demonstrates how to open a movie file 
  6. *    and play it in a window
  7. *
  8. *    Sample.h has definitions and equates
  9. *    Main.c contains the general Mac application stuff
  10. *    Movie Stuff.c has most of the interesting movie code
  11. *
  12. *    Interesting things to look at:
  13. *    How to open and close movies and players
  14. *    Calling MoviesTask from the event loop
  15. *    Playing movies at different speeds
  16. *    Checking for the end of the movie while playing
  17. *    Handling the update event for the movie window
  18. *    Using a scroll bar to control the position of the movie
  19. *    My cat in the about box
  20. *
  21. *     Rich Williams 11/90, 2/91
  22. *    Updated to 4.04 interfaces 4/91
  23. *    Updated to MPW/Think 5.0/DSG interfaces 9/91 Chris Thorman
  24. *
  25. ***************************************************/
  26.  
  27. /**************************************************
  28. *
  29. * General Info
  30. *
  31. ***************************************************/
  32. #include "Movies.h"
  33. #include "Mini Player.h"
  34.  
  35. #include <Memory.h>
  36. #include <Palettes.h>
  37. #include <Fonts.h>
  38. #include <OSEvents.h>
  39. #include <ToolUtils.h>
  40. #include <Desk.h>
  41. #include <Resources.h>
  42. #include <Sound.h>
  43.  
  44. /* MacHeaders Included */
  45.  
  46. /* The menus */
  47. MenuHandle    appleMenu, fileMenu, editMenu;
  48.  
  49. /* The Cursors */
  50. Cursor    waitCursor, pauseCursor, reverseCursor, playCursor, blinkCursor;
  51.  
  52. /* Window stuff*/
  53. WindowPtr    moovWindow;
  54. Rect        dragRect;
  55. Rect        picBounds ;
  56. Rect        picPos = {100,100, 200, 200};        /* A starting position for the window */
  57.  
  58.  
  59.  
  60. extern    Movie    m;                /* The movie.  We're only using one */
  61. extern    Rect    dispBounds;        /* The bounds of the movie */
  62. extern    Boolean trackingHand;    /* Flag used when mouse pressed in the movie */
  63.  
  64.  
  65.  
  66. /**************************************************
  67. ***************************************************
  68. *
  69. * Initialization Routines
  70. *
  71. * All of the stuff we call to get things started
  72. *
  73. ***************************************************
  74. ***************************************************/
  75.  
  76. /**************************************************
  77. *
  78. * InitMacintosh() Initialize all the managers & memory
  79. *
  80. ***************************************************/
  81. void InitMacintosh()
  82.  
  83. {
  84.     MaxApplZone();
  85.     InitGraf(&qd.thePort);
  86.     InitPalettes();
  87.     InitFonts();
  88.     FlushEvents(everyEvent, 0);
  89.     InitWindows();
  90.     InitMenus();
  91.     TEInit();
  92.     InitDialogs(0L);
  93.     InitCursor();
  94.  
  95. }
  96.  
  97. /**************************************************
  98. *
  99. * SetUpMenus()
  100. *
  101. * Reads in the menu resources and installs them
  102. *
  103. *
  104. ***************************************************/
  105. void SetUpMenus()
  106.  
  107. {
  108.     InsertMenu(appleMenu = GetMenu(appleID), 0);
  109.     InsertMenu(fileMenu = GetMenu(fileID), 0);
  110.     InsertMenu(editMenu = GetMenu(editID), 0);
  111.     DrawMenuBar();
  112.     AddResMenu(appleMenu, 'DRVR');
  113.     
  114. }
  115.  
  116. /**************************************************
  117. *
  118. * SetUpWindows()
  119. *
  120. *    Create the movie window.
  121. *
  122. ***************************************************/
  123. void SetUpWindows()
  124.  
  125. {    
  126.     /* Create the window for the picture */    
  127.     dragRect = qd.screenBits.bounds;
  128.     moovWindow = NewCWindow(0L,&picPos,(ConstStr255Param)"\p",0,4,(WindowPtr)-1L,1,0L);    /* Make a new window */
  129.     SetPort(moovWindow);
  130.  
  131. }
  132.  
  133. /**************************************************
  134. *
  135. * SetUpCursors() Gets the cursors we need
  136. *
  137. **************************************************/
  138. void SetUpCursors()
  139. {
  140.     CursHandle    hCurs;
  141.     
  142.     hCurs = GetCursor(watchCursor);
  143.     waitCursor = **hCurs;
  144.     
  145.     hCurs = GetCursor(pauseCursorID);
  146.     pauseCursor = **hCurs;
  147.  
  148.     hCurs = GetCursor(reverseCursorID);
  149.     reverseCursor = **hCurs;
  150.  
  151.     hCurs = GetCursor(playCursorID);
  152.     playCursor = **hCurs;
  153.  
  154.     hCurs = GetCursor(blinkCursorID);
  155.     blinkCursor = **hCurs;
  156. }
  157.  
  158. /**************************************************
  159. ***************************************************
  160. *
  161. *    Routines for handling menus.
  162. *
  163. ***************************************************
  164. ***************************************************/
  165.  
  166. /**************************************************
  167. *
  168. *  AdjustMenus()
  169. *
  170. *    Enable or disable the items in the Edit menu if a DA window
  171. *    comes up or goes away. Our application doesn't do anything with
  172. *    the Edit menu.
  173. *
  174. ***************************************************/
  175. void AdjustMenus()
  176. {
  177.     register WindowPeek wp = (WindowPeek) FrontWindow();
  178.     short kind = wp ? wp->windowKind : 0;
  179.     Boolean DA = kind < 0, mainVis = ((WindowPeek) moovWindow)->visible;
  180.     
  181.     enable(editMenu, 1, DA);
  182.     enable(editMenu, 3, DA);
  183.     enable(editMenu, 4, DA);
  184.     enable(editMenu, 5, DA);
  185.     enable(editMenu, 6, DA);
  186.     
  187.     enable(fileMenu, openItem, !((WindowPeek) moovWindow)->visible);
  188.     enable(fileMenu, closeItem, DA || ((WindowPeek) moovWindow)->visible);
  189.  
  190. }
  191.  
  192. static enable(MenuHandle menu, short item, Boolean ok)
  193. {
  194.     if (ok)
  195.         EnableItem(menu, item);
  196.     else
  197.         DisableItem(menu, item);
  198. }
  199.  
  200. /**************************************************
  201. *
  202. *  HandleMenu (mSelect)
  203. *
  204. *    Handle the menu selection. mSelect is what MenuSelect() and
  205. *    MenuKey() return: the high word is the menu ID, the low word
  206. *    is the menu item
  207. *
  208. ***************************************************/
  209. void HandleMenu (mSelect)
  210.  
  211. long    mSelect;
  212.  
  213. {
  214.     int            menuID = HiWord(mSelect);
  215.     int            menuItem = LoWord(mSelect);
  216.     Str255        name;
  217.     GrafPtr        savePort;
  218.     WindowPeek    frontWindow;
  219.     
  220.     switch (menuID)
  221.     {
  222.         case    appleID:                /* The Apple menu */
  223.             switch (menuItem)
  224.             {
  225.                 case    aboutItem:
  226.                     DoAboutBox();        /* Display the About Box */
  227.                     break;
  228.                     
  229.                 default:                /* It's a DA */
  230.                     GetPort(&savePort);
  231.                     GetItem(appleMenu, menuItem, name);
  232.                     OpenDeskAcc(name);
  233.                     SetPort(savePort);
  234.                     break;
  235.             }
  236.             break;
  237.     
  238.           case    fileID:                    /* The file menu */
  239.             switch (menuItem)
  240.               {
  241.                   case    openItem:
  242.                       DoOpen();            /* Open a movie */
  243.                     break;
  244.                                 
  245.                   case    closeItem:
  246.                     if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  247.                           break;
  248.               
  249.                     if (frontWindow->windowKind < 0)
  250.                           CloseDeskAcc(frontWindow->windowKind);
  251.                     else
  252.                         /* It's the movie window ( the only one we've got ) */
  253.                         /* Close it & clean up the movie stuff */
  254.                         CloseEm( (WindowPtr) frontWindow);
  255.  
  256.                     break;
  257.  
  258.                                                             
  259.                 case    quitItem:        /* Pack up and go home */
  260.                     /* Clean up if movie still there */
  261.                     if (((WindowPeek) moovWindow)->visible)
  262.                         CloseEm(moovWindow);
  263.                     ExitToShell();
  264.                     break;
  265.             }
  266.             break;
  267.                   
  268.         case    editID:
  269.             if (!SystemEdit(menuItem-1))
  270.                   SysBeep(5);
  271.             break;
  272.       }
  273. }
  274.  
  275. /**************************************************
  276. *
  277. *  CloseEm(w:WindowPtr) Closes window w 
  278. *    if w is a movie window, clean up the movie and the player
  279. *
  280. ***************************************************/
  281. void CloseEm(w)
  282.  
  283. WindowPtr w;
  284.  
  285. {
  286.     HideWindow(w);
  287.     if (w == moovWindow)    /* If its the movie window, throw out the movie */
  288.     {
  289.         CleanUpMovie();
  290.     }
  291. }
  292.  
  293. /**************************************************
  294. *
  295. *  DoAboutBox() Draws and displays the about box
  296. *
  297. ***************************************************/
  298. void DoAboutBox()
  299.  
  300. {
  301.     DialogPtr    theDialog;        /* Stuff for the about box */
  302.     short            itemHit, io;
  303.     Handle        tempHandle, soundHandle;
  304.     Rect        box;
  305.     
  306.     soundHandle = GetResource('snd ',meowID);
  307.     theDialog = GetNewDialog(aboutDlgID,0L,(WindowPtr)-1L);
  308.     
  309.     /* Highlight the default button */
  310.     SetPort(theDialog);
  311.     GetDItem(theDialog,1,&itemHit,&tempHandle,&box);
  312.     PenSize(3,3);
  313.     InsetRect(&box,-4,-4);
  314.     FrameRoundRect(&box,16,16);
  315.     
  316.     /* Wait until the button is pressed */
  317.     do
  318.         ModalDialog(0l,&itemHit);
  319.     while (itemHit != 1);
  320.     
  321.     /* Button pressed, now meow and go home */
  322.     io = SndPlay(0L,soundHandle,0);
  323.     ReleaseResource(soundHandle);
  324.     DisposDialog(theDialog);
  325. }
  326.  
  327. /**************************************************
  328. *
  329. * DoOpen()
  330. *
  331. *    Brings up standard file dialog
  332. *    and opens the movie if one has been selected
  333. *
  334. ***************************************************/
  335. void DoOpen()
  336.  
  337. {
  338.     Point    sFGwhere = { 90 ,82 };    /* Location of dialog for getfile */
  339.     SFReply    reply;                    /* Reply record from getfile */
  340.     SFTypeList    myTypeList = {'MooV'};
  341.     
  342.     SFGetFile(sFGwhere,(ConstStr255Param)"\p",0L,2,myTypeList,0L,&reply);
  343.     if (reply.good)
  344.         OpenTheMovie(reply.fName,reply.vRefNum);    
  345.  
  346. }
  347.  
  348. /**************************************************
  349. ***************************************************
  350. *
  351. *        The window routines
  352. *
  353. ****************************************************
  354. ***************************************************/
  355.  
  356. /**************************************************
  357. *
  358. * AdjustCursor()
  359. *
  360. *    Adjusts the cursor if it is in the movie window 
  361. *
  362. ***************************************************/
  363. void AdjustCursor()
  364. {
  365.     Point        pt;
  366.     WindowPeek    wPtr;
  367.     GrafPtr        savePort;
  368.     Boolean itsIn;
  369.  
  370.     if( IsMyWindow((WindowPtr) (wPtr = (WindowPeek) FrontWindow())))
  371.     {
  372.         GetPort(&savePort);
  373.         SetPort((GrafPtr) wPtr);
  374.         GetMouse(&pt);
  375.             if (PtInRect(pt,&dispBounds))    /* In the movie window? */
  376.                 MovieCursor();
  377.             else
  378.                 SetCursor(&qd.arrow);
  379.         
  380.         SetPort(savePort);
  381.     }
  382. }
  383.         
  384. /**************************************************
  385. *
  386. * IsMyWindow(w):Boolean 
  387. *    Checks if w is a valid window and mine
  388. *
  389. ***************************************************/
  390. Boolean IsMyWindow(w)
  391.  
  392. WindowPtr    w;
  393.  
  394. {
  395.     return( (w != 0L) && 
  396.              (w == moovWindow) );
  397. }
  398.     
  399. /**************************************************
  400. ***************************************************
  401. *
  402. * Event handling stuff
  403. *
  404. **************************************************** 
  405. ***************************************************/
  406.  
  407. /**************************************************
  408. *
  409. * HandleMouseDown (theEvent)
  410. *
  411. *    Take care of mouseDown events.
  412. *
  413. ***************************************************/
  414. void HandleMouseDown(theEvent)
  415.  
  416. EventRecord    *theEvent;
  417.  
  418. {
  419.     WindowPtr    theWindow;
  420.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  421.     
  422.     switch (windowCode)
  423.     {
  424.         case inSysWindow: 
  425.             SystemClick (theEvent, theWindow);
  426.             break;
  427.         
  428.           case inMenuBar:
  429.               AdjustMenus();
  430.             HandleMenu(MenuSelect(theEvent->where));
  431.             break;
  432.         
  433.           case inDrag:
  434.               if (IsMyWindow(theWindow))
  435.                     DragWindow(theWindow, theEvent->where, &dragRect);
  436.                 break;
  437.             
  438.           case inContent:
  439.               if (IsMyWindow(theWindow))
  440.                 {
  441.                     if (theWindow != FrontWindow())
  442.                       SelectWindow(theWindow);
  443.                     else
  444.                       MovieMouseDown(theWindow,theEvent->where); /* Track the mouse */
  445.                 }
  446.               break;
  447.           
  448.         case inGoAway:
  449.               if (IsMyWindow(theWindow) && TrackGoAway(theWindow, theEvent->where))
  450.                 CloseEm(theWindow);        /* Close the appropriate windows */
  451.             break;
  452.         
  453.         break;
  454.       }
  455. }
  456.  
  457. /**************************************************
  458. *
  459. * HandleMouseUp (theEvent)
  460. *
  461. *    Take care of mouseUp events for tracking the hand.
  462. *
  463. ***************************************************/
  464. void HandleMouseUp(theEvent)
  465.  
  466. EventRecord    *theEvent;
  467.  
  468. {
  469.     WindowPtr    theWindow;
  470.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  471.     
  472.     if( windowCode == inContent )
  473.         MovieMouseUp(theWindow,theEvent->where);
  474.  
  475.     trackingHand = false;
  476. }
  477.  
  478. /**************************************************
  479. *
  480. * HandleEvent()
  481. *
  482. *        The main event dispatcher. This routine should be called
  483. *        repeatedly (it  handles only one event).
  484. *
  485. ***************************************************/
  486. void HandleEvent()
  487.  
  488. {
  489.     int            ok;
  490.     EventRecord    theEvent;
  491.     WindowPtr    theWindow;
  492.     OSErr        retstat;
  493.  
  494.     HiliteMenu(0);
  495.     SystemTask ();        /* Handle desk accessories */
  496.     AdjustCursor();        /* Adjust the cursor as needed */
  497.     
  498.     /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  499.     !
  500.     ! Don't miss this!
  501.     ! MoviesTask is called from the main event loop to 
  502.     ! give time to the player
  503.     !
  504.     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  505.     MyMoviesTask();
  506.  
  507.     ok = GetNextEvent (everyEvent, &theEvent);
  508.     if (ok)
  509.       switch (theEvent.what)
  510.         {
  511.         case mouseDown:
  512.             HandleMouseDown(&theEvent);
  513.             break;
  514.             
  515.         case mouseUp:
  516.             HandleMouseUp(&theEvent);
  517.             break;
  518.  
  519.         case keyDown: 
  520.         case autoKey:
  521.             if ((theEvent.modifiers & cmdKey) != 0)
  522.             {
  523.               AdjustMenus();
  524.               HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  525.             }
  526.             break;
  527.             
  528.         case updateEvt:
  529.             /* To update the window, make the player replay the last frame */
  530.             theWindow = (WindowPtr) theEvent.message;    /* Get the window */
  531.  
  532.             BeginUpdate(theWindow);            
  533.             if(theWindow == moovWindow)
  534.                 DoMovieUpdate();
  535.             DrawControls(theWindow);
  536.             EndUpdate(theWindow);
  537.             break;
  538.             
  539.         case activateEvt:
  540.             /* Force an update event */
  541.             SetPort( (WindowPtr) theEvent.message );
  542.             InvalRect(&((WindowPtr)theEvent.message)->portRect);
  543.             break;
  544.         }
  545. }
  546.  
  547. /**************************************************
  548. *
  549. * main()
  550. *
  551. *    This is where everything happens
  552. *
  553. ***************************************************/
  554. void main()
  555.  
  556. {    
  557.     InitMacintosh();        /* Initialize everything */
  558.     SetUpMenus();
  559.     SetUpCursors();
  560.     SetUpWindows();
  561.     SetUpMovies();
  562.  
  563.     for (;;)
  564.         HandleEvent();
  565. }
  566.